package com.ling.service;

import com.ling.bean.UserTrack;
import com.ling.dao.UserTrackMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserTrackService {

    private final UserTrackMapper userTrackMapper;

    @Autowired
    public UserTrackService(UserTrackMapper userTrackMapper) {
        this.userTrackMapper = userTrackMapper;
    }

    //增加用户浏览记录
    public void saveUserTrack(UserTrack userTrack) {
        //判断是否重复添加浏览记录
        if (!existRecord(userTrack)) {
            userTrackMapper.insert(userTrack);
        }
    }

    //根据用户id获取指定用户的浏览记录
    public List<UserTrack> getUserTrackByUserId(String userId) {
        return userTrackMapper.getUserTrackByUserId(userId);
    }

    private boolean existRecord(UserTrack userTrack) {
        return !userTrackMapper.existRecord(userTrack).isEmpty();
    }
}
